#include "ProbabilisticHoughTransformLineDetector.h"

using namespace std;

ProbabilisticHoughTransformLineDetector::ProbabilisticHoughTransformLineDetector(
	double  rhoPrecision,
	double  thetaPrecision,
	int     threshold,
	double  minLineLength,
	double  maxGap) : HoughTransformLineDetector(rhoPrecision, thetaPrecision, threshold)
{

	this->minLineLength   = minLineLength;
	this->maxGap          = maxGap;
}

ProbabilisticHoughTransformLineDetector::~ProbabilisticHoughTransformLineDetector(void) {
}

vector<Line> ProbabilisticHoughTransformLineDetector::detect(const IplImage* im) {

	vector<Line> vlines;

	assert(im->nChannels == 1); //assume gray

	IplImage* canny = cvCreateImage( cvGetSize(im), 8, 1 );
	CvMemStorage* storage = cvCreateMemStorage(0);
	CvSeq* lines = 0;
	cvCanny( im, canny, 50, 200, 3 );

    lines = cvHoughLines2( canny, storage, CV_HOUGH_PROBABILISTIC, rhoPrecision, thetaPrecision, threshold, minLineLength, maxGap);
   
	for(int i = 0; i < lines->total; i++ ){
        CvPoint* line  = (CvPoint*)cvGetSeqElem(lines,i);
		vlines.push_back(Line(line[0], line[1]));
	}

	return vlines;
}

